﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_func_city_m : System.Web.UI.Page
{
    int no;

    protected void Page_Load(object sender, EventArgs e)
    {
        no = int.Parse(Request["no"]);

        this.HyperLink2.Visible = false;
        this.Panel2.Visible = true;
        this.Panel3.Visible = false;

        ctpDataContext db = new ctpDataContext();

        ct_profile cts = db.ct_profiles.Single(p => p.no == no);

        this.Label1.Text = cts.country;

        this.HyperLink2.Text = cts.title;
        this.HyperLink2.NavigateUrl = "~/city.aspx?no=" + no;

        ShowData();

        if (!IsPostBack)
        {
            var data1 = from p1 in db.ct_profiles
                        where p1.country == cts.country
                        select p1.title;

            this.DropDownList2.DataSource = data1;
            this.DropDownList2.DataBind();

            var data3 = from p in db.ct_s_profiles
                        where p.valid == true && p.ct_no == no
                        select p.title;

            this.DropDownList3.DataSource = data3;
            this.DropDownList3.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ctpDataContext db = new ctpDataContext();

        ct_profile ct = db.ct_profiles.Single(p => p.title == this.DropDownList2.SelectedValue);

        if ( ct.ct_s_amount == 0)
        {
            db.ct_profiles.DeleteOnSubmit(ct);

            datalog act = new datalog();
            act.actionlog = this.DropDownList2.SelectedValue.Trim() + "(CityName) has been deleted by " + db.users.Single(p => p.no == (int)Session["userno"]).nickname.Trim() + " on " + DateTime.Now.ToString() + ".";
            db.datalogs.InsertOnSubmit(act);

            db.SubmitChanges();
            this.msg.Text = "刪除動作已完成";
        }
        else
        {
            this.msg.Text = "資料庫仍有東西不得刪除";
        }

        ShowData();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ctpDataContext db = new ctpDataContext();

        ct_s_profile cts = db.ct_s_profiles.Single(p => p.title == this.DropDownList3.SelectedValue && p.valid == true);
        cts.valid = false;
        db.SubmitChanges();

        this.Label6.Text = this.DropDownList3.SelectedValue;

        datalog act = new datalog();
        act.actionlog = this.DropDownList3.SelectedValue.Trim() + "(HotSpot) has been deleted by " + db.users.Single(p => p.no == (int)Session["userno"]).nickname.Trim() + " on " + DateTime.Now.ToString() + ".";
        db.datalogs.InsertOnSubmit(act);
        db.SubmitChanges();

        ShowData();
    }

    private void ShowData()
    {
        ctpDataContext db = new ctpDataContext();

        ct_profile cts = db.ct_profiles.Single(p => p.no == no);

        var data = from p in db.ct_profiles
                   where p.country == cts.country
                   select new
                   {
                       title = p.title,
                       url = "~/city.aspx?no=" + p.no,
                       country = p.country
                   };

        this.GridView1.DataSource = data;
        this.GridView1.DataBind();

        var data2 = from p in db.ct_s_profiles
                    where p.valid == true && p.ct_no == no
                    select new
                    {
                        title = p.title,
                        url = "intro_m.aspx?no=" + p.no
                    };

        this.GridView2.DataSource = data2;
        this.GridView2.DataBind();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.msg.Text = "";
        this.TextBox1.Text = this.DropDownList2.SelectedValue;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        ctpDataContext db = new ctpDataContext();
        //JEFF ADDED BEGIN
        GeoLingManager glManager = new GeoLingManager(Request, Response);
        String _lang = glManager.getLang();
        DatabaseUtil dbUtil = new DatabaseUtil();
        //Get user's preferred langugage in int
        int lang = dbUtil.getLanguageNo(_lang);
        ct_profile ct = db.ct_profiles.Single(p => p.title == this.DropDownList2.SelectedValue);
        ct_language_text relativeText = db.ct_language_texts.Single(
            x => x.post_no == ct.no && 
            x.from_table_no == (int)DatabaseUtil.fromTables.ct_profile_title && 
            x.language_no == lang);
        if (relativeText != null)
            relativeText.text = this.TextBox1.Text.Trim();
        else
            Response.Write("CAN NOT FIND TRANSLATED TEXT FOR CITY: "+ct.title);
        //JEFF END 
        ct.title = this.TextBox1.Text.Trim();
        db.SubmitChanges();

        datalog act = new datalog();
        act.actionlog = this.TextBox1.Text.Trim() + "(CityName) has been modified by " + db.users.Single(p => p.no == (int)Session["userno"]).nickname.Trim() + " on " + DateTime.Now.ToString() + ".";
        db.datalogs.InsertOnSubmit(act);
        db.SubmitChanges();

        ShowData();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.HyperLink2.Visible = false;
        this.Panel2.Visible = true;
        this.Panel3.Visible = false;
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        this.HyperLink2.Visible = true;
        this.Panel2.Visible = false;
        this.Panel3.Visible = true;
    }
}
